from bs4 import BeautifulSoup
import requests, sys, os, time

REVEL_SESSION = ""
cookies = {"REVEL_SESSION": REVEL_SESSION}
contestId = sys.argv[1]
cur = ""

while True:
    try:
        response = requests.get(
            f"https://atcoder.jp/contests/{contestId}/submissions/me/status/json",
            cookies=cookies,
        )
        lst = tuple(response.json()["Result"])
        tmp = lst[-1]
        if tmp == cur:
            continue
        soup = BeautifulSoup(response.json()["Result"][tmp]["Html"], "html.parser")
        # print(soup)
        tst = tuple(i.text for i in soup.find_all("td"))
        # print(tst)
        if (
            soup.find("td").find("span")["title"] == "Waiting for Judging"
            or soup.find("td").find("span")["title"] == "Judging"
            or str(tst[0]).find("/") != -1
        ):
            time.sleep(5)
            continue
        cur = tmp
        os.system(
            f'notify-send --urgency={'normal' if tst[0] == 'AC' else 'critical'} "{soup.find('td').find('span')['title']}" "Submission ID: {tmp}\\nResource usage: {tst[1]} / {tst[2]}\\nScore: {response.json()['Result'][tmp]['Score']}"'
        )
    except:
        pass
    time.sleep(5)
